package com.twodstar.photobattle;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;

import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.ActionBar.TabListener;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.facebook.Session;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.parse.ParseFacebookUtils;
import com.parse.ParseUser;
import com.parse.PushService;
import com.twodstar.photobattle.slidemenu.BaseActivity;
import com.twodstar.photobattle.slidemenu.SlideMenuFragment;
import com.twodstar.photobattle.R;


public class MainActivity extends BaseActivity {
	
	
	


	public static final int SPLASH = 0;
	public static final int SELECTION = 1;
    public static final int SETTINGS = 2;
    public static final int BATTLES = 3;
    public static final int BEST = 4;
    public static final int ABOUT = 5;
    public static final int FRAGMENT_COUNT = ABOUT +1;
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		PushService.setDefaultPushCallback(this, MainActivity.class);                           
 		ParseUser currentUser = ParseUser.getCurrentUser();
 		
 		if(PhotoBattleApplication.Instance().isOnline() == false)
 		{
 			Log.i("game","no connection!"); 	        
	 		showFragment(SPLASH);
 		}
 		else if ((currentUser != null) && ParseFacebookUtils.isLinked(currentUser)) 
 		{
 			Session session = ParseFacebookUtils.getSession();
 	        if (session != null && session.isOpened()) 
 	        { 	       	
 	 	    	
 	    		int BattleCount = currentUser.getInt("TotalBattle");
 	    		if(BattleCount > 0)
 	    		{
 	    			Log.i("game","showFragment(BATTLES, false);");
 	    			showFragment(BATTLES); 	    			
 	    		}
 	    		else 
 	    		{ 	    			
 	    			Log.i("game","ShowSelection()"); 	
 	    			ShowSelection();
 	    		} 	 	    	
 	           
 	        }
 	        else
 	        {
 	        	Log.i("game","session is not open"); 	        
 	 			showFragment(SPLASH);
 	        } 			
 			
 			
 		}
 		else
 		{
 			Log.i("game","ask to log in");
 			showFragment(SPLASH);
 		}
		 
	}
	
	public void showFragment(int ID)
	{
		if(getSupportActionBar().isShowing() == false)
			getSupportActionBar().show();
		switch(ID)
		{
		case SELECTION:
			switchContent(Fragment_NewBattle.Instance());
			break;
		
		case SPLASH:
			switchContent(Fragment_Login.Instance());
			getSupportActionBar().hide();
			break;
			
		case BATTLES:
			switchContent(Fragment_Battles.Instance());
			break;
			
		case BEST:
			switchContent(Fragment_Best.Instace());			
			Fragment_Best.Instace().GetBestListAfterGettingFrdList();
	    	
			break;
			
		case ABOUT:
			switchContent(Fragment_About.Instance());
			break;
			
					
		
		}
		//switchContent(fragment);
	}
	
	public void ShowSelection()
    {
		if(getSupportActionBar().isShowing() == false)
			getSupportActionBar().show();
		switchContent(Fragment_NewBattle.Instance());
    	Session S = ParseFacebookUtils.getSession();
    	if(S != null)
    	{
    		Fragment_NewBattle.Instance().makeMeRequest(S);
    		Fragment_NewBattle.Instance().ShowPlayerInfo();
    	}
    }
	
	@Override
	public void onBackPressed() 
	{
		// TODO Auto-generated method stub
		
		if(getSlidingMenu() != null)
			getSlidingMenu().toggle();
		else
			super.onBackPressed();
		
	}
	
	public void showSettingsFragment()
	{
		
	}
	
	
	
	public void switchContent(Fragment fragment) 
	{
		mContent = fragment;
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame, fragment)
		.commit();
		getSlidingMenu().showContent();
	}
	
	@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
    	Log.i("game", "test activity result requestcode" + requestCode + " resultcode " + resultCode );
        super.onActivityResult(requestCode, resultCode, data);
        
        final int PARSE_REQUEST_CODE = 32665;
        if(requestCode == PARSE_REQUEST_CODE)
        {
        	ParseFacebookUtils.finishAuthentication(requestCode, resultCode, data);        	
        }
        else
        {
        	//uiHelper.onActivityResult(requestCode, resultCode, data);
        }
        
    }
	
	

}
